BaseGenerator   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 18
dl 0
loc 33
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A generateByModel 0 4 3
B constructor 0 25 7
1
import Markov from './Markov';
2
import Static from './Static';
3
4
export default class BaseGenerator {
5
    // eslint-disable-next-line sonarjs/cognitive-complexity
6
    constructor(fatum) {
7
        this.fatum = fatum;
8
        if (this.constructor.model) {
9
            const { type } = this.constructor.model;
10
11
            if (type === 'markov') {
12
                this._markov = new Markov(this.constructor.model, this.constructor.safeLength);
13
            }
14
15
            if (type === 'merged_types') {
16
                this._generators = {};
17
                for (const typeName of Object.keys(this.constructor.model.types)) {
18
                    const model = this.constructor.model.types[typeName];
19
20
                    if (model.type === 'markov') {
21
                        this._generators[typeName] = new Markov(model);
22
                    }
23
24
                    if (model.type === 'static') {
25
                        this._generators[typeName] = new Static(model);
26
                    }
27
                }
28
            }
29
        }
30
    }
31
32
    generateByModel(type, ...params) {
33
        if (this._markov) return this._markov.generate(type, ...params);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
34
        if (this._generators) return this._generators[type].generate(...params);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
Complexity Best Practice introduced by
There is no return statement if this._generators is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
35
    }
36
}
37